package com.ruoyi.informfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.informfrontend.domain.SysUser;
import com.ruoyi.informfrontend.domain.SysUserNeed;
import com.ruoyi.informfrontend.mapper.SysUserMapper;
import com.ruoyi.informfrontend.mapper.SysUserNeedMapper;
import com.ruoyi.informfrontend.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-17
 */
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserNeedMapper sysUserNeedMapper;

    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    @Override
    public SysUser selectSysUserByUserId(Long userId) {
        return sysUserMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询用户列表
     *
     * @param sysUser 用户
     * @return 用户
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户
     *
     * @param sysUser 用户
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser) {
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUser.setUserName(sysUser.getPhonenumber());
        int i = sysUserMapper.insertSysUser(sysUser);
        changeUserType(sysUser);
        return Math.toIntExact(sysUser.getUserId());
    }

    /**
     * 修改用户
     *
     * @param sysUser 用户
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser sysUser) {

        if (sysUser.getUserType() != null) {
            SysUser user = sysUserMapper.selectSysUserByUserId(sysUser.getUserId());
            if (!user.getUserType().equals(sysUser.getUserType())) {
                changeUserType(sysUser);
            }
        }

        sysUser.setUpdateTime(DateUtils.getNowDate());
        try {
            /*修改需求用户信息*/
            SysUserNeed sysUserNeed = new SysUserNeed();
            BeanUtils.copyProperties(sysUser, sysUserNeed);
            if (sysUser.getNickName() != null) {
                sysUserNeed.setCompanyName(sysUser.getNickName());
            }
            sysUserNeedMapper.updateSysUserNeed(sysUserNeed);
        } catch (Exception ignored) {
        }
        return sysUserMapper.updateSysUser(sysUser);
    }


    /**
     *改变身份和角色
     */
    private void changeUserType(SysUser sysUser) {
        switch (sysUser.getUserType()) {
            case "01":
                sysUserMapper.insertUserRole(sysUser.getUserId(), 100);
                break;
            case "02":
                sysUserMapper.insertUserRole(sysUser.getUserId(), 101);
                break;
            case "03":
                sysUserMapper.insertUserRole(sysUser.getUserId(), 102);
                break;
            case "04":
                sysUserMapper.insertUserRole(sysUser.getUserId(), 103);
                /*需求方信息创建*/
                SysUserNeed need = new SysUserNeed();
                need.setUserId(sysUser.getUserId());
                sysUserNeedMapper.insertSysUserNeed(need);
                break;
        }
    }

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserIds(Long[] userIds) {
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户主键
     * @return 结果
     */
    @Override
    public int deleteSysUserByUserId(Long userId) {
        return sysUserMapper.deleteSysUserByUserId(userId);
    }

    @Override
    public SysUser selectSysUserByPhone(String phone) {
        return sysUserMapper.selectSysUserByPhone(phone);
    }

    /**
     * 根据手机号修改密码
     *
     * @param sysUser
     */
    @Override
    public int updateSysUserByPhone(SysUser sysUser) {
        return sysUserMapper.updateSysUserByPhone(sysUser);
    }
}
